class Solution
{
public:
    bool isNStraightHand(vector<int> &hand, int groupSize)
    {
        if (hand.size() % groupSize != 0)
        {
            return false;
        }
        if (groupSize == 1)
        {
            return true;
        }
        sort(hand.begin(), hand.end());
        int groupsCount = hand.size() / groupSize;
        int nextStart = 0;
        for (int round = 0; round < groupsCount; ++round)
        {
            int curCard = hand[nextStart];
            hand[nextStart] = -1;
            bool gotNextStart = false;
            int thisGroupSize = 1;
            for (int i = nextStart + 1; i < hand.size(); ++i)
            {
                if (thisGroupSize == groupSize)
                {
                    if (!gotNextStart)
                    {
                        nextStart = i;
                    }
                    break;
                }
                if (hand[i] == -1)
                {
                    continue;
                }
                if (!gotNextStart && hand[i] == curCard)
                {
                    nextStart = i;
                    gotNextStart = true;
                }
                if (hand[i] == curCard + 1)
                {
                    ++curCard;
                    ++thisGroupSize;
                    hand[i] = -1;
                }
                else if (hand[i] > curCard + 1)
                {
                    return false;
                }
            }
            if (thisGroupSize != groupSize)
            {
                return false;
            }
        }
        return true;
    }
};